﻿#include "window/win32/ssFrameWnd.h"
#include "window/win32/ssWin32Msg.h"
#include "console/ssConsole.h"
#include "opengl/ssOpenGLSupport.h"
#include "MainWorld.h"
#include <tchar.h>

//窗口
StarSeeker::ssFrameWnd		*	g_pFrameWnd = 0;
//消息分类
StarSeeker::ssWin32Message	*	g_pMSG = 0;
//渲染线程
HANDLE				g_hDrawThread = 0;
//
CMainWorld		*	g_pWorld = 0;

//渲染线程
//DWORD WINAPI DrawRun(LPVOID param)
//{
//	std::cout<<"渲染线程开始"<<std::endl;
//	LPDIRECT3DDEVICE9 pDevice = (LPDIRECT3DDEVICE9)param;
//	while(!g_bAppClose)
//	{
//		pDevice->Clear(0, //矩形总数（prect为0这里必须填0）
//			0,//矩形指针（全屏）
//			D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, //清除的地方
//			0x006e7882, //用于清除的颜色
//			1.0f,// Z缓冲区域数值
//			0 //蒙板缓冲区数值
//			);
//		pDevice->BeginScene(); //开始绘制
//		{
//		}
//		pDevice->EndScene(); //结束绘制
//		pDevice->Present(0, 0, 0, 0); //交换前后台
//		//::Sleep(1);
//	}
//	::CloseHandle(g_hDrawThread);
//	g_hDrawThread = 0;
//	return 0;
//}

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//建立窗口
	g_pFrameWnd = new StarSeeker::ssFrameWnd;
	if (!g_pFrameWnd->BuildWindow(hInstance, L"StarSeekerShow"))
	{
		SS_CONSOLE->Print("create window error!\n");
		return 0;
	}
	else
	{
		SS_CONSOLE->Print("create window success\n");
	}
	//::ShowCursor(FALSE);

	SS_OPENGL->InitGLDevice(g_pFrameWnd->GethWnd(), 0, g_pFrameWnd->GetWidth(), g_pFrameWnd->GetHeight());
	SS_OPENGL->InitGLState(false);

	g_pWorld = new CMainWorld;
	g_pMSG = (StarSeeker::ssWin32Message*)g_pWorld;
	ssFrameWnd::s_pMSG = g_pMSG;

	//g_hDrawThread = ::CreateThread(0, 0, DrawRun, g_pFrameWnd->GetD3DDevice(), 0, 0);

	HRESULT hr = 0;
	//消息循环
	MSG msg;
	while (!StarSeeker::ssFrameWnd::s_windowClose)
	{
		if(::PeekMessage(&msg, g_pFrameWnd->GethWnd(), 0, 0, PM_REMOVE))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}

		g_pWorld->Render();

		::Sleep(10);
	}

	//::Sleep(2000);
	g_pMSG = 0;
	delete g_pFrameWnd;
	//delete g_pWorld;
	return 0;
}
